<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Backtest extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model("repos/symbolmodel", "SymbolModel");
    }

    public function index()
    {
        $data['arrSymbol'] = $this->SymbolModel->getSymbol();
        $data['isLoadCalendar'] = true;
        $this->renderView('backtest/index', $data);
    }

    public function dobacktest()
    {
        //check date valid
        $startDate = trim($this->input->post("txtstartdate", true));
        $endDate = trim($this->input->post("txtenddate", true));
        if ($startDate == '') {
            $dateError = "<span class='error'><strong>Start date</strong> was not valid !</span>";
            $this->load->view('common/script', array('message' => $dateError));
            return;
        }
        if ($endDate == '') {
            $dateError = "<span class='error'><strong>End date</strong> was not valid !</span>";
            $this->load->view('common/script', array('message' => $dateError));
            return;
        }
        try {
            if (!$this->checkDate($startDate)) {
                $dateError = "<span class='error'><strong>Start date</strong> was not valid !</span>";
                $this->load->view('common/script', array('message' => $dateError));
                return;
            }
            if (!$this->checkDate($endDate)) {
                $dateError = "<span class='error'><strong>End date</strong> was not valid !</span>";
                $this->load->view('common/script', array('message' => $dateError));
                return;
            }
            
            $startDate = date('Y-m-d H:i:s', strtotime("$startDate - 4 months"));
            $startDate = new DateTime($startDate, new DateTimeZone(TIMEZONE));
            $endDate = new DateTime($endDate, new DateTimeZone(TIMEZONE));
            if ($endDate <= $startDate) {
                throw new Exception('E_0001');
            }
        }
        catch (exception $ex) {
            $dateError = "";
            if ($ex->getMessage() == 'E_0001') {
                $dateError = "<span class='error'><strong>Start date</strong> must be less than <strong>end date</strong> !</span>";
            } else {
                $dateError = "<span class='error'><strong>Start date</strong> or <strong>end date</strong> was not valid !</span>";
            }
            $this->load->view('common/script', array('message' => $dateError));
            return;
        }

        //check Banks valid
        $cmpA = $this->input->post("cbxcmpa", true);
        $cmpB = $this->input->post("cbxcmpb", true);
        if ($cmpA == -1 || $cmpB == -1) {
            $dateError = "<span class='error'>Please choose <strong>Banks</strong> !</span>";
            $this->load->view('common/script', array('message' => $dateError));
            return;
        } elseif ($cmpA == $cmpB) {
            $dateError = "<span class='error'><strong>Banks</strong> were the same !</span>";
            $this->load->view('common/script', array('message' => $dateError));
            return;
        }

        $arrSymbol = $this->SymbolModel->getSymbol();

        $data['bankA'] = $this->getUrlValFromId($arrSymbol, $cmpA);
        $data['bankB'] = $this->getUrlValFromId($arrSymbol, $cmpB);

        $arrCmpA = $this->SymbolModel->getSymbolData($startDate, $endDate, $cmpA);
        $arrCmpB = $this->SymbolModel->getSymbolData($startDate, $endDate, $cmpB);

        if (empty($arrCmpA) || empty($arrCmpB)) {
            $dateError = "<span class='error'>" . NO_RECORD_FOUND . " for <strong>{$data['bankA']}</strong> and <strong>{$data['bankB']}</strong> from <strong>{$last1Year->format('d-M-Y')}</strong> to <strong>{$curDate->format('d-M-Y')}</strong> !</span>";
            $this->load->view('common/script', array('message' => $dateError));
        } else {
            $this->load->library('calculatordata');
            $arrCmp = $this->calculatordata->getDataBackTest($arrCmpA, $arrCmpB, $data['bankA'], $data['bankB']);
            if (is_null($arrCmp) || empty($arrCmp)) {
                $this->load->view('common/script', array('message' => '<span class=\'error\'>No trade found !</span>'));
            } else {
                $this->load->view('backtest/table', array('data' => $arrCmp));
            }
        }

        return;
    }
}
